หลังจากติดปัญหาเรื่อง BPMN Engine แล้วพบว่า Service Task ถ้าทำเป็น Web Service แล้วข้อมูล Request ในส่วนของ Body มันหายไปครับ หลังจากไล่ไปไล่มา โดยดูจาก
- Forum ของ Camunda
- ตัว Code มาไล่แกะไปจาก Stack Trace
- หรือลอง Debug Request ที่ส่งเข้าไปใน JSON-SERVER ครับ
ในที่สุดก็พบปัญหาแล้วครับ เนื่องจากตัว BPMN Engine ที่ผมเลือกได้ใช้ Tomcat HTTP 1.1 connector ซึ่งมีการเปิด transfer encoding ให้เป็น Mode = "Chunked" ซึ่งมีการส่งข้อมูลเป็นชิ้นๆครับ และที่สำคัญ ถ้าทำ Request แบบนี้มันจะไม่ระบุขนาดของ Request ไปด้วยครับ ซึ่งนี่แหละที่ทำให้ JSON-Server มันมอง Request-Body ว่าไม่ได้ส่งอะไรมาครับ งงอยู่ตั้ง 2 สัปดาห์ครับ
ถ้าจะไปแก้เป็นตัว HTTP 1.0 มันก็ไม่ควรครับ เพราะจะทำให้ตัว Camunda BPMN Engine ทำงานผิดปรกตืในส่วนอื่นได้ครับ หลังจากค้นๆดูแล้ว ตัว JSON-Server มันยอมให้เราทำ Middleware มาครอบ เพื่อจัดการ Request แทนครับ สำหรับ Code ที่เชียนจะใช้ Code ต่อจาก Blog ตอนก่อนครับ โดยเพิ่มการดัก Request ที่เป็นประเภท Chunked
- Request - ON ให้รอรับ Chunked ที่เข้ามาครับ
let body = [];
req.on('data', function(chunk) {
   console.log("Received body data:");
   body.push(chunk);
});- Request - END เมื่อรับ Request มาหมดแล้ว ให้ทำอะไรต่อครับ สำหรับผมให้แปลงเป็น JSON ครับ
res.on('end', function() {
   let data   = Buffer.concat(body);
   let schema = JSON.parse(data);
   req.raw = schema;
   console.log(req.raw);
   req.body = req.raw;
   console.log("Isarray : "+ Array.isArray(req.body))
   //console.log(util.inspect(req, {depth: null}));
   if (req.method === 'POST') {
      req.body.createdAt = Date.now()
   }
   //Get Request Body
   console.log("req.body is " + req.body)
   console.log("req.raw is " + req.rawBody)
});- ส่งต่อให้ตัว JSON-Server คำสั่ง Next()
- Code ที่ใช้ทั้งหมดครับ โดยตัว Request แบบ Chunked ให้ทำงานตอนมีการส่งข้อมูลแบบ POST / PUT และ PATCH ครับ และปรับให้ Request - ON กับ Request - END ทำงานต่อกัน
server.use(jsonServer.bodyParser)
server.use((req, res, next) => {
  console.log("========================");
  if ((req.method === 'POST') || (req.method === 'PATCH') || (req.method === 'PUT')) {
    let body = [];
    req.on('data', function(chunk) {
      console.log("Received body data:");
      //data += chunk.toString();
      body.push(chunk);
    }).on('end', function() {
      //req.raw = Buffer.concat(body).toString();
      let data   = Buffer.concat(body);
      let schema = JSON.parse(data);
      req.raw = schema;
      console.log(req.raw);
      req.body = req.raw;
      console.log("Isarray : "+ Array.isArray(req.body))
      //console.log(util.inspect(req, {depth: null}));
      if (req.method === 'POST') {
        req.body.createdAt = Date.now()
      }
      //Get Request Body
      console.log("req.body is " + req.body)
      console.log("req.raw is " + req.rawBody)
    });
  }
   // Continue to JSON Server router
  next() 
})Code ของ Middleware ทั้งหมดครับ
| const jsonServer = require('json-server') | |
| const server = jsonServer.create() | |
| const router = jsonServer.router('db.json') | |
| const middlewares = jsonServer.defaults() | |
| const util = require('util') | |
| console.log("I am PingkungA") | |
| // Set default middlewares (logger, static, cors and no-cache) | |
| server.use(middlewares) | |
| // Add custom routes before JSON Server router | |
| server.get('/echo', (req, res) => { | |
| res.jsonp(req.query) | |
| }) | |
| // To handle POST, PUT and PATCH you need to use a body-parser | |
| // You can use the one used by JSON Server | |
| server.use(jsonServer.bodyParser) | |
| server.use((req, res, next) => { | |
| console.log("========================"); | |
| //var data = ''; | |
| if ((req.method === 'POST') || (req.method === 'PATCH') || (req.method === 'PUT')) { | |
| let body = []; | |
| req.on('data', function(chunk) { | |
| console.log("Received body data:"); | |
| body.push(chunk); | |
| }).on('end', function() { | |
| //req.raw = Buffer.concat(body).toString(); | |
| let data = Buffer.concat(body); | |
| let schema = JSON.parse(data); | |
| req.raw = schema; | |
| console.log(req.raw); | |
| req.body = req.raw; | |
| console.log("Isarray : "+ Array.isArray(req.body)) | |
| //console.log(util.inspect(req, {depth: null})); | |
| if (req.method === 'POST') { | |
| req.body.createdAt = Date.now() | |
| } | |
| //Get Request Body | |
| console.log("req.body is " + req.body) | |
| console.log("req.raw is " + req.rawBody) | |
| }); | |
| } | |
| // Continue to JSON Server router | |
| next() | |
| }) | |
| // Use default router | |
| server.use(router) | |
| server.listen(3000, () => { | |
| console.log('JSON Server is running') | |
| }) | 
Reference
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.



